#include <bits/stdc++.h>

using namespace std;

const int months[13] = {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

int is_leap(int year)
{
    if(year % 4 == 0 && year % 100 || year % 400 == 0)
        return 1;
    return 0;
}

int get_days(int y, int m)
{
    if(m == 2)
        return months[m] + is_leap(y);
    return months[m];
}

int main()
{
    int T;

    cin >> T;
    while(T--)
    {
        int y, m, d, a;

        cin >> y >> m >> d >> a;

        d += a;
        while(d > get_days(y, m))
        {
            d -= get_days(y, m);
            m ++;
            if(m == 13)
                m = 1, y++;
        }
        printf("%04d-%02d-%02d\n", y, m, d);
    }

    return 0;
}
